package phoenics.manpads.common.mybatis.page;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.factory.ObjectFactory;
import org.apache.ibatis.reflection.property.PropertyTokenizer;
import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
import java.util.List;

/**
 * @author Phoenics Chow
 * on 2018年12月11日
 */
public class Page  implements ObjectWrapper {
    private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(Page.class);
    private int pageNo = 1;// 页码，默认是第一页
    private int pageSize = 15;// 每页显示的记录数，默认是15
    private int totalRecord;// 总记录数
    private int totalPage;// 总页数
    private List<Object> result= Lists.newArrayList();

    public Page() {
    }
    @JsonIgnore
    public Object get(PropertyTokenizer prop) {
        throw new UnsupportedOperationException();
    }

    public void set(PropertyTokenizer prop, Object value) {
        throw new UnsupportedOperationException();
    }

    public String findProperty(String name, boolean useCamelCaseMapping) {
        throw new UnsupportedOperationException();
    }
    @JsonIgnore
    public String[] getGetterNames() {
        throw new UnsupportedOperationException();
    }
    @JsonIgnore
    public String[] getSetterNames() {
        throw new UnsupportedOperationException();
    }
    @JsonIgnore
    public Class<?> getSetterType(String name) {
        throw new UnsupportedOperationException();
    }
    @JsonIgnore
    public Class<?> getGetterType(String name) {
        throw new UnsupportedOperationException();
    }

    public boolean hasSetter(String name) {
        throw new UnsupportedOperationException();
    }

    public boolean hasGetter(String name) {
        throw new UnsupportedOperationException();
    }

    public MetaObject instantiatePropertyValue(String name, PropertyTokenizer prop, ObjectFactory objectFactory) {
        throw new UnsupportedOperationException();
    }
    @JsonIgnore
    public boolean isCollection() {
        return true;
    }

    public void add(Object element) {
       this.result.add(element);

    }

    public <E> void addAll(List<E> element) {
        PageList<E> pl=(PageList<E>)element;
        int count = pl.getTotal();
        int offset = pl.getOffset();
        int pageSize = pl.getLimit();
        this.setPageNo(offset/pageSize + 1);
        this.setPageSize(pageSize);
        this.setTotalRecord(count);
        this.setTotalPage(totalRecord % pageSize == 0 ? totalRecord / pageSize : totalRecord / pageSize + 1);
        this.result.clear();
        this.result.addAll(element);
        //element.clear();

    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public List<?> getData(){
        return this.result;
    }

}
